import request from "@/utils/request";

function getDictPromise(query) {
  return new Promise((reslove, reject) => {
    const result = request({
      url: "/dict/data/type/" + query,
      method: "get"
    });
    if (result) {
      reslove(result);
    } else {
      reject([]);
    }
  });
}
export async function getDictData(dictType) {
  const { data: dictData } = await getDictPromise(dictType);
  dictData.forEach(v => {
    v.label = v.dictLabel;
    v.value = v.dictValue;
  });
  return dictData;
}

// 根据类型获取字典数据并处理 label 返回一个数组
export async function dictTypeFun(type, t) {
  if (type) {
    let str = [];
    const res = await getDictData(t);
    type.split(",").forEach(v1 => {
      res.forEach(v2 => {
        if (v2.value === v1) {
          str.push(v2.label);
        }
      });
    });
    return str;
  }
}

// 根据类型获取字典数据并处理 label 返回一个数组
export async function dictTypeFunList(type, t) {
  if (type) {
    let str = [];
    const res = await getDictData(t);
    type.forEach(v1 => {
      let newArr = ["1", "2", "3", "4", "5", "6", "7", "8", "9"];
      if (!newArr.includes(v1)) {
        str.push(v1);
      } else {
        res.forEach(v2 => {
          if (v2.value === v1) {
            str.push(v2.label);
          }
        });
      }
    });
    return str;
  }
}

export function getDictLabel(dictType, arr) {
  let strArr = [];
  if (dictType) {
    const dictData = dictType.split(",");
    dictData.forEach(v => {
      arr.forEach(v1 => {
        if (v === v1.value) {
          strArr.push(v1.label);
        }
      });
    });
  }
  return strArr;
}
